import {
  $digest,
  $init
} from '../../lib/page.data'
import {
  $request,
  Session
} from '../../lib/page.auth'

const {
  regeneratorRuntime
} = global
//获取应用实例
let api = require('../../utils/api.js');
let http = require('../../utils/http.js');
let util = require('../../utils/util.js');

// pages/indexBuy/indexBuy.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    price_list: [],
    modalShow: false,
    modalShow: false,
    setprice_list: '',
    nowId: 0,
    price_id: 0,
    opunit: api.opunit,
    picker_price: ['请设置价格', '500', '600', '700', '800', '900', '1000', '1100', '1200'],
    picker_index: 0
  },

  userSkillDetailNav: function(e) {
    let id = e.currentTarget.dataset.id;
    console.log(id);
    wx.navigateTo({
      url: '/pages/indexBuy/indexBuy?id=' + id + '&user_id=' + this.data.user_id
    })
  },
  contactTap: function(e) {
    if (this.data.user_info.id == this.data.user_id) {
      wx.showToast({
        title: '不要联系自己啦',
        icon: 'none'
      });
      return false;
    }
    wx.navigateTo({
      url: '/pages/msgDetail/msgDetail?chater_id=' + this.data.user_info.id + '&user_id=' + this.data.user_id +
        '&chater_name=' + this.data.user_info.name + '&chater_thumb=' + this.data.user_info.thumb,
    })
  },
  audioPlay: function(e) {
    console.log(this.data.user_info.audio);
    if (this.data.onOff) {
      this.audioCtx.play();
      this.data.onOff = !this.data.onOff;
    } else {
      this.audioCtx.pause();
      this.data.onOff = !this.data.onOff;
    }
    this.setData({
      onOff: this.data.onOff
    });
  },
  bannerChange: function(e) {
    console.log(e.detail.current);
    this.setData({
      current: e.detail.current
    })
  },
  buyNow: function() {
    if (this.data.user_info.id == this.data.user_id) {
      wx.showToast({
        title: '您给自己下单干甚么-_-|||',
        icon: 'none'
      });
      return false;
    }
    wx.navigateTo({
      url: '/pages/indexBuyOrder/indexBuyOrder?pid=' + this.data.price_detail.id,
    })
  },
  shareBtn: function() {
    var that = this;
    wx.showShareMenu({
      success: function(res) {
        console.log(1, res);
        that.onShareAppMessage();
      },
      fail: function() {
        console.log(2);
      }
    })
  },
  cancelTap: function() {
    this.setData({
      modalShow: false,
      modal2Show: false,
    })
  },
  confirmTap: function(e) {
    var gameSetting = this.data.setprice_list;
    console.log(gameSetting);
    var time = e.detail.value.time;
    if (time == '') {
      wx.showModal({
        title: '提示',
        content: '请输入价格',
        showCancel: false,
        success(res) {}
      })
      return;
    }
    var that = this;
    gameSetting.price = time;
    var game = 'price_list[' + that.data.nowId + ']';
    that.setData({
      modalShow: false
    });
    if (time) {
      that.setData({
        [game]: gameSetting
      });
    }
    return;
    // http.post(api.editplay_post, data, function (res) {
    //   if (res.data.code == 0) {
    //     wx.showToast({
    //       title: res.data.msg,
    //       icon: 'none'
    //     });
    //     return;
    //   }
    //   else {
    //     gameSetting.tuan_price = time;
    //     var game = 'price_list[' + that.data.nowId + ']';
    //     that.setData({
    //       modalShow: false
    //     });
    //     if (time) {
    //       that.setData({
    //         [game]: gameSetting
    //       });
    //     }
    //     //console.log(this.data);
    //   }
    // }, '设置中...');

  },
  timesetTap: function(e) {
    var id = e.currentTarget.dataset.id;
    var s_id = e.currentTarget.dataset.sid;
    var gameSetting = this.data.price_list[id];
    this.setData({
      modalShow: true,
      nowId: id,
      price_id: s_id,
      setprice_list: gameSetting
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad(options) {
    // this.setData({user_id: options.user_id});

    var that = this;
    var token = wx.getStorageSync('token');
    var priceList = that.data.picker_price;
    //请求详情数据
    //api.editplay = host + "user/editplay";
    http.get(api.editplay, {
      token: token,
      user_id: wx.getStorageSync("user_info").data.id
    }, function(res) {
      console.log(priceList[6]);
      var price_detail = res.data.data;
      console.log(price_detail);
      if (res.data.code == 1) {
        for (var i = 0; i < price_detail.length; i++) {
          if (price_detail[i]["tuan_type"] == 1) {
            price_detail[i]["pickerID"] = 0
          } else {
            for (var j = 0; j < priceList.length; j++) {
              if (priceList[j] == parseFloat(price_detail[i]["price"])) {
                price_detail[i]["pickerID"] = j
              }
            }
          }

        }
        that.setData({
          price_list: price_detail
        });
      } else {
        util.msg('请求接口失败！');
      }
    });
  },
  // 价格设置
  pickerPrice: function(e) {
    var that = this;
    var price = e.currentTarget.dataset.price;
    var old_price = e.currentTarget.dataset.oldprice;
    var id = e.currentTarget.dataset.sid;
    var tuan_type = e.currentTarget.dataset.tuantype;
    var now_type;
    var nowPrice = "";
    var temppostionlist = that.data.price_list;
    for (var i = 0; i < temppostionlist.length; i++) {
      if (temppostionlist[i]["id"] == id) {
        temppostionlist[i]["pickerID"] = e.detail.value;
        temppostionlist[i]["price"] = that.data.picker_price[e.detail.value]
      }
    }

    that.setData({
      price_list: temppostionlist
    })
    if (e.detail.value == 0){
      nowPrice = old_price;
      now_type = 1;
    }else{
      nowPrice = parseFloat(that.data.picker_price[e.detail.value]).toFixed(2);
      now_type =2
    }
    var data = {
      token: wx.getStorageSync('token'),
      id: id,
      price: nowPrice,
      tuan_type: now_type
    };
    http.post(api.change_type, data, function(res) {
      if (res.data.code == 1) {
        that.onLoad();
        wx.showToast({
          title: res.data.msg,
          icon: 'none'
        });
      }
    })
  },
  async startOrder(e) {
    var that = this;
    var tuan_type = e.currentTarget.dataset.type;
    if (e.currentTarget.dataset.price == e.currentTarget.dataset.oldprice) {
      wx.showToast({
        title: '请先设置价格',
        icon: 'none'
      });
      var temppostionlist = that.data.price_list;
      for (var i = 0; i < temppostionlist.length; i++) {
        if (temppostionlist[i]["id"] == e.currentTarget.dataset.sid) {
          temppostionlist[i]["tuan_type"] = 1;

          break;
        }
      }
      that.setData({
        price_list: temppostionlist
      })
    } else {
      //价格设置
      var nowType;
      if (tuan_type == 1) {
        nowType = 2
      } else {
        nowType = 1
      }
      var data = {
        token: wx.getStorageSync('token'),
        bool: e.detail.value,
        id: e.currentTarget.dataset.sid,
        price: e.currentTarget.dataset.price,
        old_price: e.currentTarget.dataset.oldPrice,
        tuan_type: nowType
      };
      http.post(api.change_type, data, function(res) {
        if (res.data.code == 1) {
          that.onLoad();
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          });
        }
      })
    }




  },
  async endOrder(e) {
    var that = this;
    var data = {
      token: wx.getStorageSync('token'),
      bool: e.detail.value,
      id: e.currentTarget.dataset.sid
    };
    http.post(api.tuan_type_end, data, function(res) {

      if (res.data.code == 0) {
        that.onLoad();
        return;
      } else {
        wx.showToast({
          title: res.data.msg,
          icon: 'none'
        });
      }
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    this.audioCtx = wx.createAudioContext('voice');
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {
    return {
      title: '闪约陪玩邀请您一起来玩游戏，大批陪玩师等候您'
    }
  }
});